package semantic_compute.relation_extraction.yago_extraction;

import algorithm.config.HumanLanguage;
import algorithm.nlp.corenlp.CoreNLPFactory;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeCoreAnnotations;
import edu.stanford.nlp.util.CoreMap;

import java.util.Collection;
import java.util.List;

/**
 * Created by julianzliu on 4/16/2017.
 */
public class DependencyParserTest {


    //private Annotation document;
    private StanfordCoreNLP pipeline;

    public DependencyParserTest(HumanLanguage language){
        this.pipeline = new CoreNLPFactory().getPipeline(language);
    }

    public Annotation parse(String text){
        Annotation document = new Annotation(text);
        this.pipeline.annotate( document );
        return document;
    }


    public void inteprete(Annotation document){
        List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);

        for(CoreMap sentence: sentences) {
            // traversing the words in the current sentence
            // a CoreLabel is a CoreMap with additional token-specific methods
            /*
            for (CoreLabel token: sentence.get(CoreAnnotations.TokensAnnotation.class)) {
                // this is the text of the token
                String word = token.get(CoreAnnotations.TextAnnotator.class);
                // this is the POS tag of the token
                String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
                // this is the NER label of the token
                String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
                System.out.println("word=" + word + " pos=" + pos + " ne=" + ne);
            }
            */

            // this is the parse tree of the current sentence
            Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
            tree.pennPrint();

            // this is the Stanford dependency graph of the current sentence
            SemanticGraph dependencies = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class); //CollapsedCCProcessedDependenciesAnnotation.class);

            dependencies.prettyPrint();

            dependencies.typedDependencies();


            /*
            IndexedWord vertex = dependencies.getNodeByIndexSafe(currentTokenIdx);
            List<SemanticGraphEdge> edges = new ArrayList<SemanticGraphEdge>();
            edges.addAll(dependencies.incomingEdgeList(vertex));
            edges.addAll(dependencies.outgoingEdgeList(vertex));
            */

            //ArrayList<GrammarRelation> retList = new ArrayList<GrammarRelation>();
            for(SemanticGraphEdge e:dependencies.edgeListSorted())
            {
                System.out.println(e.getRelation().toString());
            }

            Collection<IndexedWord> roots = dependencies.getRoots();
            for(IndexedWord root: roots){
                traversal_dp_tree( root );
            }

        }
    }

    public void traversal_dp_tree(IndexedWord root){
        System.out.println(root.originalText());

    }



    /*******************************************************
     * MAIN
     */
    public static void main(String[] args){
        String text;
        //text = "Barack Obama is the president of USA. He was born in Hawaii";
        //text = "Where Obama President was Born?";
        text = "Michelle is the wife of Obama, Who is the President of USA. ";
        DependencyParserTest parser = new DependencyParserTest(HumanLanguage.ENGLISH);
        Annotation document = parser.parse( text );
        parser.inteprete( document );
    }

}
